package com.project.controller;

import com.project.bean.PrimaryPostBean;
import com.project.bean.UserBean;
import com.project.service.IPrimaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("primary")
public class PrimaryPostController {
    @Autowired
    private IPrimaryService service;

    @RequestMapping("/findAll")
    public List<PrimaryPostBean> findAll(){
        return service.findAll();
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(PrimaryPostBean primary, HttpServletRequest request){
        UserBean user = (UserBean)request.getSession().getAttribute("loginUser");
        primary.setUser(user);
        service.add(primary);
        return "ok";
    }

    @RequestMapping("/findById")
    public PrimaryPostBean findById(int id){
        return service.findById(id);
    }
}
